<?php
/**
 * Class that operate on table 'deportista'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2013-11-23 21:12
 */
class DeportistaMySqlDAO implements DeportistaDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return DeportistaMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM deportista WHERE id_deportista = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM deportista';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM deportista ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param deportista primary key
 	 */
	public function delete($id_deportista){
		$sql = 'DELETE FROM deportista WHERE id_deportista = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id_deportista);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param DeportistaMySql deportista
 	 */
	public function insert($deportista){
		$sql = 'INSERT INTO deportista (nombres_deportista, apellidos_deportista, fecha_nacimiento, lugar_nacimiento, nacionalidad, estatura, peso, foto_deprtista, id_deporte, id_evento_dep, estado_deportista) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($deportista->nombresDeportista);
		$sqlQuery->set($deportista->apellidosDeportista);
		$sqlQuery->set($deportista->fechaNacimiento);
		$sqlQuery->set($deportista->lugarNacimiento);
		$sqlQuery->set($deportista->nacionalidad);
		$sqlQuery->setNumber($deportista->estatura);
		$sqlQuery->set($deportista->peso);
		$sqlQuery->set($deportista->fotoDeprtista);
		$sqlQuery->setNumber($deportista->idDeporte);
		$sqlQuery->setNumber($deportista->idEventoDep);
		$sqlQuery->set($deportista->estadoDeportista);

		$id = $this->executeInsert($sqlQuery);	
		$deportista->idDeportista = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param DeportistaMySql deportista
 	 */
	public function update($deportista){
		$sql = 'UPDATE deportista SET nombres_deportista = ?, apellidos_deportista = ?, fecha_nacimiento = ?, lugar_nacimiento = ?, nacionalidad = ?, estatura = ?, peso = ?, foto_deprtista = ?, id_deporte = ?, id_evento_dep = ?, estado_deportista = ? WHERE id_deportista = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($deportista->nombresDeportista);
		$sqlQuery->set($deportista->apellidosDeportista);
		$sqlQuery->set($deportista->fechaNacimiento);
		$sqlQuery->set($deportista->lugarNacimiento);
		$sqlQuery->set($deportista->nacionalidad);
		$sqlQuery->setNumber($deportista->estatura);
		$sqlQuery->set($deportista->peso);
		$sqlQuery->set($deportista->fotoDeprtista);
		$sqlQuery->setNumber($deportista->idDeporte);
		$sqlQuery->setNumber($deportista->idEventoDep);
		$sqlQuery->set($deportista->estadoDeportista);

		$sqlQuery->setNumber($deportista->idDeportista);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM deportista';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByNombresDeportista($value){
		$sql = 'SELECT * FROM deportista WHERE nombres_deportista = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByApellidosDeportista($value){
		$sql = 'SELECT * FROM deportista WHERE apellidos_deportista = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByFechaNacimiento($value){
		$sql = 'SELECT * FROM deportista WHERE fecha_nacimiento = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByLugarNacimiento($value){
		$sql = 'SELECT * FROM deportista WHERE lugar_nacimiento = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByNacionalidad($value){
		$sql = 'SELECT * FROM deportista WHERE nacionalidad = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEstatura($value){
		$sql = 'SELECT * FROM deportista WHERE estatura = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPeso($value){
		$sql = 'SELECT * FROM deportista WHERE peso = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByFotoDeprtista($value){
		$sql = 'SELECT * FROM deportista WHERE foto_deprtista = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByIdDeporte($value){
		$sql = 'SELECT * FROM deportista WHERE id_deporte = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByIdEventoDep($value){
		$sql = 'SELECT * FROM deportista WHERE id_evento_dep = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEstadoDeportista($value){
		$sql = 'SELECT * FROM deportista WHERE estado_deportista = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByNombresDeportista($value){
		$sql = 'DELETE FROM deportista WHERE nombres_deportista = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByApellidosDeportista($value){
		$sql = 'DELETE FROM deportista WHERE apellidos_deportista = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByFechaNacimiento($value){
		$sql = 'DELETE FROM deportista WHERE fecha_nacimiento = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByLugarNacimiento($value){
		$sql = 'DELETE FROM deportista WHERE lugar_nacimiento = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByNacionalidad($value){
		$sql = 'DELETE FROM deportista WHERE nacionalidad = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEstatura($value){
		$sql = 'DELETE FROM deportista WHERE estatura = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPeso($value){
		$sql = 'DELETE FROM deportista WHERE peso = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByFotoDeprtista($value){
		$sql = 'DELETE FROM deportista WHERE foto_deprtista = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByIdDeporte($value){
		$sql = 'DELETE FROM deportista WHERE id_deporte = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByIdEventoDep($value){
		$sql = 'DELETE FROM deportista WHERE id_evento_dep = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEstadoDeportista($value){
		$sql = 'DELETE FROM deportista WHERE estado_deportista = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return DeportistaMySql 
	 */
	protected function readRow($row){
		$deportista = new Deportista();
		
		$deportista->idDeportista = $row['id_deportista'];
		$deportista->nombresDeportista = $row['nombres_deportista'];
		$deportista->apellidosDeportista = $row['apellidos_deportista'];
		$deportista->fechaNacimiento = $row['fecha_nacimiento'];
		$deportista->lugarNacimiento = $row['lugar_nacimiento'];
		$deportista->nacionalidad = $row['nacionalidad'];
		$deportista->estatura = $row['estatura'];
		$deportista->peso = $row['peso'];
		$deportista->fotoDeprtista = $row['foto_deprtista'];
		$deportista->idDeporte = $row['id_deporte'];
		$deportista->idEventoDep = $row['id_evento_dep'];
		$deportista->estadoDeportista = $row['estado_deportista'];

		return $deportista;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return DeportistaMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>